package astudy.networking.tcp;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

import org.apache.commons.io.IOUtils;

public class FileReceiver {

	private String addr;
	private int port;
	
	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public FileReceiver(String addr, int port) {
		
		this.addr = addr;
		this.port = port;
	}
	
	public void save(File file) {
		
		try {
			System.out.println(this.getPort() + ":" + file.getAbsolutePath());
			Socket client = new Socket(this.getAddr(), this.getPort());
			FileOutputStream fos = new FileOutputStream(file);
			InputStream is = new BufferedInputStream(client.getInputStream());
			IOUtils.copy(is, fos);
			is.close();
			fos.close();
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
